<?php
/**
 * 
 *
 * @author Administrator
 */
include_once dirname(__FILE__)."/basedao.php";
include_once dirname(__FILE__)."/../objects/chapter.php";
class ChapterSDAO extends BaseDAO{
	public function insertChapter($chapter){
		$this->getConn()->query("LOCK TABLES chapters WRITE");
		$sqlstr = "INSERT INTO chapters(chapter_name, chapter_description) VALUES(?,?)";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("ss",
				$chapter->getChapterName(),
				$chapter->getChapterDescription());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	public function delChapter($chapter_id){
		$this->getConn()->query("LOCK TABLES chapters WRITE");
		$sqlstr = "DELETE FROM chapters WHERE chapter_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i",
				$chapter_id);
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	public function getAllChapters(){
		$chapters = array();
		$this->getConn()->query("LOCK TABLES chapters READ");
		$sqlstr = "SELECT chapter_id, chapter_name, chapter_description FROM chapters ORDER BY chapter_id";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->execute();
		$stmt->bind_result($chapter_id, $chapter_name, $chapter_description);
		while($stmt->fetch()){
			$chapter = new Chapter();
			$chapter->setChapterId($chapter_id);
			$chapter->setChapterName($chapter_name);
			$chapter->setChapterDescription($chapter_description);
			array_push($chapters, $chapter);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $chapters;
	}
	public function getFirstChapter(){		
		$this->getConn()->query("LOCK TABLES chapters READ");
		$sqlstr = "SELECT chapter_id, chapter_name, chapter_description FROM chapters ORDER BY chapter_id LIMIT 0,1";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->execute();
		$stmt->bind_result($chapter_id, $chapter_name, $chapter_description);
		$stmt->fetch();
		$chapter = new Chapter();
		$chapter->setChapterId($chapter_id);
		$chapter->setChapterName($chapter_name);
		$chapter->setChapterDescription($chapter_description);
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $chapter;
	}
}

?>
